#This script estimates the DSL models for Tunisia and Egypt
#It produces two .rds files with the estimated models for Tunisia and Egypt
library(dsl)

data_final_masress <- readRDS("data/output/cos_sims_dsl/masressdata_final.rds")
data_final_turess <- readRDS("data/output/cos_sims_dsl/turessdata_final.rds")

out_time_masress <- dsl(model = "lm",
                formula = scoreint_gpt ~ as.factor(yearmon) - 1,
                predicted_var = "scoreint_gpt",
                prediction = "cos_sim2",
                data = data_final_masress)


summary(out_time_masress)

# Extract coefficients from your model output
coefficients_masress <- out_time_masress[["coefficients"]]

# Create a data frame with the years and their corresponding coefficients
coef_df_masress <- data.frame(
  yearmon = gsub("as.factor\\(yearmon\\)", "", names(coefficients_masress)),
  coefficient = as.numeric(coefficients_masress)
)

out_time_turess <- dsl(model = "lm",
                       formula = scoreint_gpt ~ as.factor(yearmon) - 1,
                       predicted_var = "scoreint_gpt",
                       prediction = "cos_sim2",
                       data = data_final_turess)


summary(out_time_turess)

# Extract coefficients from your model output
coefficients_turess <- out_time_turess[["coefficients"]]

# Create a data frame with the years and their corresponding coefficients
coef_df_turess <- data.frame(
  yearmon = gsub("as.factor\\(yearmon\\)", "", names(coefficients_turess)),
  coefficient = as.numeric(coefficients_turess)
)

coef_df_masress$country_name <- "masress"
coef_df_turess$country_name <- "turess"
all_data <- rbind(coef_df_masress, coef_df_turess)
saveRDS(all_data, "data/output/cos_sims_dsl/mastun_estimation_data_yearmon.rds")


out_time_masress <- dsl(model = "lm",
                        formula = scoreint_gpt ~ as.factor(year) - 1,
                        predicted_var = "scoreint_gpt",
                        prediction = "cos_sim2",
                        data = data_final_masress)


summary(out_time_masress)

# Extract coefficients from your model output
coefficients_masress <- out_time_masress[["coefficients"]]

# Create a data frame with the years and their corresponding coefficients
coef_df_masress <- data.frame(
  year = gsub("as.factor\\(year\\)", "", names(coefficients_masress)),
  coefficient = as.numeric(coefficients_masress)
)

out_time_turess <- dsl(model = "lm",
                       formula = scoreint_gpt ~ as.factor(year) - 1,
                       predicted_var = "scoreint_gpt",
                       prediction = "cos_sim2",
                       data = data_final_turess)


summary(out_time_turess)

# Extract coefficients from your model output
coefficients_turess <- out_time_turess[["coefficients"]]

# Create a data frame with the years and their corresponding coefficients
coef_df_turess <- data.frame(
  year = gsub("as.factor\\(year\\)", "", names(coefficients_turess)),
  coefficient = as.numeric(coefficients_turess)
)

coef_df_masress$country_name <- "masress"
coef_df_turess$country_name <- "turess"
all_data <- rbind(coef_df_masress, coef_df_turess)
saveRDS(all_data, "data/output/cos_sims_dsl/mastun_estimation_data_year.rds")